package com.dao.impl;

import com.dao.UserDAO;
import com.db.ConnectionManager;
import com.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class UserDAOImpl implements UserDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 用户登录验证
    @Override
    public User findByAccountAndPassword(String account, String password) {
        String sql = "SELECT * FROM users WHERE account = ? AND userPwd = ?";

        try {
            return jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
                User user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setAccount(rs.getString("account"));
                user.setUserName(rs.getString("userName"));
                user.setUserPwd(rs.getString("userPwd"));
                user.setSex(rs.getString("sex"));
                user.setIntroduction(rs.getString("introduction"));
                return user;
            }, account, password);
        } catch (EmptyResultDataAccessException e) {
            return null;  // 找不到用户返回null
        }
    }

    //检查手机号是否已存在
    @Override
    public User findStudentByAccount(String account){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "SELECT * FROM users WHERE account = ? ";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setAccount(rs.getString("account"));
                user.setUserPwd(rs.getString("userPwd"));
                user.setUserName(rs.getString("userName"));
                user.setSex(rs.getString("sex"));
                user.setIntroduction(rs.getString("introduction"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, rs);
        }
        return user;
    }

    // 添加用户
    @Override
    public int insert(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "INSERT INTO users (account, userPwd, userName, sex, introduction) " +
                    "VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getUserPwd());
            pstmt.setString(3, user.getUserName());
            pstmt.setString(4, user.getSex());
            pstmt.setString(5, user.getIntroduction());

            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("SQL Error: " + e.getMessage()); // 打印 SQL 错误信息
        } finally {
            ConnectionManager.closeAll(conn, pstmt, null);
        }
        return result;
    }

    // 修改用户信息
    @Override
    public int update(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "UPDATE users SET account=?, userPwd=?, userName=?, sex=?, introduction=? " +
                    "WHERE userId=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getUserPwd());
            pstmt.setString(3, user.getUserName());
            pstmt.setString(4, user.getSex());
            pstmt.setString(5, user.getIntroduction());
            pstmt.setInt(6, user.getUserId());
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, null);
        }
        return result;
    }
}